var pageSize=10;
var params = {
    id:'',
    remark:'',
    start: 1,
    size: pageSize,
    branchName: '',
    status:'',
    approvalStatus:'',
    ebbId:'',
    userId:''
};

$(window).ready(function () {
    //加载物流信息
    loadLogistics();

    var lock = false;
    $("form[name=imporexcelform]").submit(function () {
        if (lock) {
            alert("不能重复提交");
            return;
        }
        lock = true;
        $(".btn-info").attr('disabled', true);
        $(this).ajaxSubmit(options);
        return false; //阻止表单默认提交
    });
    //检索
    $('#submit').click(function () {
        var branchName = $('#storeName').val().trim();
        params.branchName = branchName;
        params.status = $('#audit-status').val().trim();
        loadLogistics();
    });
    //分页跳转
    $('#go-index').click(function () {
        var pageIndex = $('#page-index').val();
        params.start = parseInt(pageIndex);
        loadLogistics();
    });

});
//加载物流信息
function loadLogistics() {
    $.ajax({
        url: '/ebb/admin/apply/?'+"storeName="+params.branchName+"&status="+params.status+"&start=1"+"&size="+params.size,
        type: 'get',
        success: function (result) {
            if (result.data.length > 0) {
                $("#total-page").text(result.totalPages);
                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: result.totalPages,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function (event, page) {
                        params.start = page;
                        $.ajax({
                            url: '/ebb/admin/apply/?'+"storeName="+params.branchName+"&status="+params.status+"&start="+params.start
                            +"&size="+params.size,
                            type: 'get',
                            success: function (result) {
                                console.log(result);
                                $.each(result.data, function (i, item) {
                                        if(item.approvalStatus==-1){
                                            item.approvalStatus="待审核";
                                        }else if(item.approvalStatus==0){
                                            item.approvalStatus="审核失败";
                                        }else{
                                            item.approvalStatus="审核通过";
                                        }

                                    // //删除物流信息
                                    // $('#logistics-delete-' + i).click(function () {
                                    //     if (!confirm('您确定要删除吗？')) {
                                    //         return;
                                    //     }
                                    //     deleteLogisticsBusiness(item.logisticsBusinessId);
                                    // });
                                });
                                $.templates("#logistics-list-tmpl").link("#logistics-list-data", result.data);
                                $('#page-index').val(page);
                                $('#total-page').text(result.totalPages);
                                $('#total-count').text(result.totalCount);
                                $.each(result.data, function (i, item) {



                                    // //删除物流信息
                                    // $('#logistics-delete-' + i).click(function () {
                                    //     if (!confirm('您确定要删除吗？')) {
                                    //         return;
                                    //     }
                                    //     deleteLogisticsBusiness(item.logisticsBusinessId);
                                    // });
                                });
                            }
                        });
                    },
                    startPage: params.start
                });
            } else {
                $("#logistics-list-data").html("<tr><td colspan='13'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 401) {
                //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                // redirect_login();
                ShowTip("你还没有登录，请先登录！", "success", 1000);
                setTimeout(function () {
                    window.location = "login.html";
                }, 1000);
            }
        }
    });
}

function getData(id,userId,ebbId) {
    params.id=id;
    params.userId=userId;
    params.ebbId=ebbId;
}


var lock = false;
$("form[name=agree-form]").submit(function () {
    params.approvalStatus='1';

    if (lock) {
        alert("不能重复提交");
        window.location = "auditManager.html";
    }
    lock = true;
    params.remark= $("#agree_text").val();
    $.ajax({
        url: '/ebb/admin/apply/',
        data:JSON.stringify(params),
        contentType:"application/json",
        type: 'put',
        success: function (data) {
            console.log(data);
            if (!data.error) {
                ShowTip("已确定通过！", "success", 1000);
                setTimeout(function () {
                    window.location = "login.html";
                }, 1000);
                window.location = "auditManager.html";
            } else {

                if (data.errorMessage) {
                    ShowTip(data.errorMessage, "error", 1000);
                    setTimeout(function () {
                        window.location = "login.html";
                    }, 1000);
                }
            }
        },
        error: function (error) {

        }

    });
    return false; //阻止表单默认提交
});

var lock1 = false;
$("form[name=refuse-form]").submit(function () {
    params.approvalStatus='0';
    if (lock1) {
        alert("不能重复提交");
        window.location = "auditManager.html";
    }
    lock1 = true;
    params.remark= $("#refuse_text").val();
    $.ajax({
        url: '/ebb/admin/apply/',
        data:JSON.stringify(params),
        contentType:"application/json",
        type: 'put',
        success: function (data) {
            console.log(data);
            if (!data.error) {
                ShowTip("已确定拒绝！", "success", 1000);
                setTimeout(function () {
                    window.location = "login.html";
                }, 1000);
                window.location = "auditManager.html";
            } else {

                if (data.errorMessage) {
                    ShowTip(data.errorMessage, "error", 1000);
                    setTimeout(function () {
                        window.location = "login.html";
                    }, 1000);
                }
            }
        },
        error: function (error) {

        }

    });
    return false; //阻止表单默认提交
});